<?php

/**
 * This is the model class for table "business".
 *
 * The followings are the available columns in table 'business':
 * @property integer $id
 * @property string $title
 * @property string $descripton
 * @property integer $square_meters
 * @property integer $country
 * @property integer $district
 * @property integer $local
 * @property integer $category_id
 * @property integer $activ
 * @property integer $user_id
 * @property string $date_add
 * @property string $date_expire
 * @property integer $amount
 * @property string $img1
 * @property string $img2
 * @property string $img3
 * @property string $img4
 * @property string $img5
 */
class Business extends CActiveRecord {

    public $lat;
    public $long;
    public $title_en;
    public $title_de;
    public $description_en;
    public $description_de;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Business the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'business';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array(' title, descripton, square_meters, country, district, local,  activ, user_id, date_add, date_expire, amount,lat,long', 'required'),
            array(' square_meters,  category_id, activ, user_id, amount', 'numerical', 'integerOnly' => true),
            array('title, img1, img2, img3, img4, img5', 'length', 'max' => 255),
            array('descripton', 'length', 'max' => 500),
            array('date_add, date_expire', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, descripton, square_meters, country, district, local, category_id, activ, user_id, date_add, date_expire, amount, img1, img2, img3, img4, img5', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            			

        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'title' => Yii::t('models', 'Titulo'),
            'descripton' => Yii::t('models', 'Descricao'),
            'square_meters' => Yii::t('models', 'Área(m2)'),
            'country' => Yii::t('models', 'País'),
            'district' => Yii::t('models', 'Distrito'),
            'local' => Yii::t('models', 'Local'),
            'category_id' => Yii::t('models', 'Categoria'),
            'activ' => Yii::t('models', 'Ativo'),
            'user_id' => Yii::t('models', 'Utilizador'),
            'date_add' => Yii::t('models', 'Adicionado'),
            'date_expire' => Yii::t('models', 'Expira'),
            'amount' => Yii::t('models', 'Preço'),
            'img1' => 'Img1',
            'img2' => 'Img2',
            'img3' => 'Img3',
            'img4' => 'Img4',
            'img5' => 'Img5',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    
    public function searchFrontEnd() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('activ', 1,0);
        $criteria->compare('date_expire >', date('Y-m-d'));
  
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
$us = new RWebUser();
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('descripton', $this->descripton, true);
        $criteria->compare('square_meters', $this->square_meters);
        $criteria->compare('country', $this->country);
        $criteria->compare('district', $this->district);
        $criteria->compare('local', $this->local);
        $criteria->compare('category_id', $this->category_id);
        //$criteria->compare('activ', 1);
        $criteria->compare('user_id', !Rights::getAuthorizer()->isSuperuser($us->getId()) === false ? '':$us->getId());
        $criteria->compare('date_add', $this->date_add, true);
        //$criteria->compare('date_expire',">= ". date('Y-m-d'), true);
        $criteria->compare('amount', $this->amount);


        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
    public function __call($name, $parameters) {
        parent::__call($name, $parameters);
    }
    

public function behaviors()
{
    return array(
        
        array(
            'class'=>'ext.seo.components.SeoRecordBehavior',
            'route'=>'model/view',
            'params'=>array('id'=>$this->id, 'name'=>$this->title),
        ),
    );
}

}